package org.gitman.agent.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextPane;

import org.gitman.agent.platform.Agent;
import org.gitman.agent.platform.PlatformManager;
import org.gitman.agent.platform.Simulation;

public class EditAgentDialog extends JDialog{

	public EditAgentDialog(PlatformManager platformManager, final Agent agent, Component parent) {
		setLayout(new BorderLayout());
		final JComboBox<String> combo = new JComboBox<String>();
		List<Agent> agents = platformManager.getAllAgents();
		final Map<String, Agent> name2agent = new TreeMap<String, Agent>();
		for(Agent a : agents){
			name2agent.put(a.getName(), a);
			combo.addItem(a.getName());
		}		
		add(combo, BorderLayout.NORTH);
		combo.setSelectedItem(agent.getName());
		
		final JTextPane text = new JTextPane();
		text.setBorder(BorderFactory.createTitledBorder("Description"));
		text.setText(agent.getDescription());
		text.setEditable(false);
		add(text, BorderLayout.CENTER);
		
		combo.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox)e.getSource();
		        Agent selectedAgent = name2agent.get((String)cb.getSelectedItem());
		        text.setText(selectedAgent.getDescription());
		        text.repaint();
			}
		});		
		
		JPanel bottomPanel = new JPanel(new FlowLayout());
		JButton ok = new JButton("ok");
		bottomPanel.add(ok);
		ok.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Agent selectedAgent = name2agent.get((String)combo.getSelectedItem());
		        if(!selectedAgent.getName().equals(agent.getName())){
		        	Agent a2set = selectedAgent.getInstance();
		        	a2set.setColor(agent.getColor());
		        	a2set.setCurrentPoint(agent.getCurrentPoint());
		        	a2set.setEndPoint(agent.getEndPoint());
		        	a2set.setStartPoint(agent.getStartPoint());
		        	a2set.setState(agent.getState());
		        	a2set.setIteration(agent.getIteration());
		        	Simulation simulation = agent.getSimulation();
		        	a2set.setSimulation(simulation);
		        	if(simulation.isInitialized()){
		        		simulation.pause();
		        		simulation.restart();
		        	}
		        	simulation.getAgentManager().removeAgent(agent);
		        	simulation.getAgentManager().addAgent(a2set);
		         	
		        	EditAgentDialog.this.setVisible(false);
					EditAgentDialog.this.dispose();
		        }
			}
		});
		JButton cancel =new JButton("cancel");
		cancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				EditAgentDialog.this.setVisible(false);
				EditAgentDialog.this.dispose();
			}
		});
		bottomPanel.add(cancel);
		add(bottomPanel, BorderLayout.SOUTH);
		
		setAlwaysOnTop(true);
		setTitle("Edit Agent");
		setSize(200, 150);
		setLocation(parent.getLocationOnScreen());
		setVisible(true);
	}

	
}
